package com.easy.api.factory.file;

import com.easy.api.annotation.FileModelGenerate;
import com.easy.api.bean.file.FileType;
import com.easy.api.factory.file.service.HeaderFileTemplate;
import com.easy.api.utils.ApplicationContextUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.core.annotation.AnnotationUtils;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;


/**
 * @Author liuyuan
 * @Date 2020/5/3 1:19 下午
 * @Version 1.0
 */
public class FileInvocationProxy implements InvocationHandler {
    private Class<?> interfaceClass;

    public Object bind(Class<?> cls) {

        this.interfaceClass = cls;
        return Proxy.newProxyInstance(cls.getClassLoader(), new Class[]{interfaceClass}, this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        ApplicationContext applicationContext = ApplicationContextUtils.getApplicationContext();
        FileModelGenerate fileApiGenerate = getFileApiGenerate(method);
        FileType type = fileApiGenerate.type();
        String serviceName = type.getServiceName();
        HeaderFileTemplate headerFileTemplate = (HeaderFileTemplate)applicationContext.getBean(serviceName);
        headerFileTemplate.buildHeaderTemplate(fileApiGenerate,method,args);
        headerFileTemplate.generateFile();
        return null;
    }

    private FileModelGenerate getFileApiGenerate(Method method) {
        return AnnotationUtils.findAnnotation(method, FileModelGenerate.class);
    }



   /* public static void main(String[] args) throws IllegalAccessException, IOException {
        *//*FileSystemView fsv = FileSystemView.getFileSystemView();
        File com = fsv.getHomeDirectory();
        System.out.printf(com.getPath());*//*
        ArrayList<String> strings = new ArrayList<>();
        strings.add("tom");
        strings.add("luck");
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("name","liuyuan");
        stringObjectHashMap.put("th",strings);
        String sl = render("templates/","template", stringObjectHashMap);
        System.out.printf(sl);



    }*/



}
